package com.nyist.controller;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nyist.pojo.ResultData;
import com.nyist.pojo.User;
import com.nyist.service.UserService;
import com.nyist.util.ExcelUtil;
import com.nyist.util.POIPptToHtml;
import com.nyist.util.UploadUtils;


/**
 * @author hu
 *
 * 2017年9月20日
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {

	@Autowired
	private UserService userSerivce;
	
	private static Logger log = Logger.getLogger(UserController.class);

	@SuppressWarnings("rawtypes")
	@RequestMapping( value = "uploadPpt")	
	public String uploadPpt(HttpServletRequest request, ModelMap modelMap) {
		String name = null;
		try {
			UploadUtils upload = new UploadUtils();
			String path = "/eastRoute";
			List resultInfo = upload.uploadFile(request, path);
			String pptPath = (String)resultInfo.get(3);
			String pptImage = (String)resultInfo.get(2);
			name = POIPptToHtml.pptToHtml(pptPath, pptImage + "//ppt");
		} catch (Exception e) {
			log.error("上传PPT文件出错" + e.getMessage());
		}
//		return "http://127.0.0.1/" + name;
		return "redirect:/user/showUser.htm";
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" } )
	@RequestMapping("/importExcel")
	public String importExcel(HttpServletRequest request, ModelMap modelMap) {
		try {
			String path = "/eastRoute";
			UploadUtils upload = new UploadUtils();
			List resultInfo = upload.uploadFile(request, path);
/*			String filePath = (String) resultInfo.get(3);
			List<String> nameList = (List<String>) resultInfo.get(5);
			List<User> userList = setUsers(filePath, nameList);
			if (null != userList && userList.size() > 0) {
				userSerivce.insertUsers(userList);
			}*/
		} catch (Exception e) {
			log.error("导入Excel出现异常：" + e.toString());
		}
		return "redirect:/user/showUser.htm";
	}
	
	private List<User> setUsers(String path, List<String> nameList) {
		List<User> users = new ArrayList<User>();
		InputStream in = null;
		try {
			in = new FileInputStream("I:\\" + path);
			List<List<Object>> obj = ExcelUtil.readExcel(nameList.get(0), in);
			if (null != obj && obj.size() > 0) {
				for (int i=1; i<obj.size();i++) {
					User user = new User();
					List<Object> list = obj.get(i);
					user.setId(list.get(0).toString());
					user.setUsername(list.get(1).toString());
					user.setPassword(list.get(2).toString());
					user.setAddress(list.get(3).toString());
					user.setZipcode(list.get(4).toString());
					user.setPhone(list.get(5).toString());
					user.setEmail(list.get(6).toString());
					users.add(user);
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return users;
	}
	
	@RequestMapping("/showUser")
	public String showUser(ModelMap modelMap) {
		User user = userSerivce.getUserById("1");
		modelMap.put("user", user);
		return "user/showUser";
	}
	
	@RequestMapping("/list")
	public String list(ModelMap modelMap) {
		User user = userSerivce.getUserById("1");
		modelMap.put("user", user);
		return "user/imageUpload";
	}
	
	@SuppressWarnings("rawtypes")
	@RequestMapping("/upload")
	@ResponseBody
	public ResultData upload(HttpServletRequest request) {
		ResultData result = new ResultData();
		try {
			UploadUtils upload = new UploadUtils();
			List resultInfo = upload.uploadFile(request, "/eastRoute");
			result.setList(resultInfo);
			result.setCode(200).setSuccess(true);
			return result;
		} catch (Exception e) {
			result.setCode(500).setSuccess(false);
			return result;
		}
	}
	
}
